package com.song.codesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.song.codesandbox.utils.DockerUtils;

import java.util.List;

public class DockerDemo {
    public static void main(String[] args) throws InterruptedException {

        // 获取默认的Docker客户端
        DockerClient dockerClient = DockerUtils.getDefaultDockerClient();
        String nginxImage = "nginx:latest";

        // 下载镜像
        pullImage(dockerClient, nginxImage);

        // 创建容器
        CreateContainerResponse containerResponse = createContainer(dockerClient, nginxImage);
        String containerId = containerResponse.getId();

        // 查看容器状态
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
        for (Container container : containerList) {
            System.out.println("容器状态: " + container.getImage());
        }

        // 启动容器
        StartContainerCmd startContainerCmd = dockerClient.startContainerCmd(containerId);
        startContainerCmd.exec();
        System.out.println("容器启动: " + startContainerCmd);

        // 查看日志
        dockerClient.logContainerCmd(containerId)
                .withStdErr(true)
                .withStdOut(true)
                .exec(new LogContainerResultCallback() {
                    @Override
                    public void onNext(Frame item) {
                        System.out.println("hahaha");
                        System.out.println("日志: " + item.toString());
                        super.onNext(item);
                    }
                })
                .awaitCompletion();

        // 删除容器
//        dockerClient.removeContainerCmd(containerId).withForce(true).exec();

        // 删除镜像
//        dockerClient.removeImageCmd(nginxImage).exec();

    }

    /**
     * 获取默认的Docker客户端配置对象
     *
     * @return
     */
    public static DefaultDockerClientConfig getDefaultDockerClientConfig() {
        return new DefaultDockerClientConfig.Builder()
                .withDockerTlsVerify(false)
                // 这里填最上面填的ip端口号，ip换成服务器ip
                .withDockerHost("tcp://81.70.164.218:2375")
                // docker API版本号，可以用docker version查看
                .withApiVersion("1.43")
                .build();
    }

    /**
     * 获取默认的Docker客户端
     *
     * @param dockerClientConfig
     * @return
     */
    public static DockerClient getDockerClient(DockerClientConfig dockerClientConfig) {
        return DockerClientBuilder.getInstance(dockerClientConfig).build();
    }

    /**
     * 下载镜像
     *
     * @param dockerClient Docker客户端
     * @param imageName    镜像名称
     * @throws InterruptedException
     */
    public static void pullImage(DockerClient dockerClient, String imageName) throws InterruptedException {
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(imageName);
        pullImageCmd.exec(new PullImageResultCallback() {
            @Override
            public void onNext(PullResponseItem item) {
                System.out.println("下载镜像状态: " + item.getStatus());
                super.onNext(item);
            }
        }).awaitCompletion();
        System.out.println("镜像下载完成");
    }

    /**
     * 创建容器
     *
     * @param dockerClient Docker客户端
     * @param imageName    镜像名称
     * @return
     * @throws InterruptedException
     */
    public static CreateContainerResponse createContainer(DockerClient dockerClient, String imageName) {
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(imageName);
        CreateContainerResponse containerResponse = containerCmd
                .withCmd("echo Hello Docker", "Hello Docker")
                .exec();
        System.out.println("容器创建成功: " + containerResponse.getId());
        return containerResponse;
    }

}
